﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Windows.Forms;
using PefStudio.Base;
using PefStudio.Services;
using PefStudio.StartUp;

namespace PefStudio.Windows
{
    [Export(Bootstrapper.MainWindowContract, typeof(Form))]
    public partial class MainWindow : AbstractDockDocument, IPartImportsSatisfiedNotification
    {
        public static CompositionContainer CompositionContainer = Bootstrapper.Container;
        public const string MainMenuContract = "PefStudio.MainWindow.MainMenu";
        public const string MainToolStripsContract = "PefStudio.MainWindow.ToolStrips";
        public const string MainStatusBarContract = "PefStudio.MainWindow.StatusBar";
        public const string LoadedCommandsContract = "PefStudio.MainWindow.LoadedCommands";
        public const string ClosingCommandsContract = "PefStudio.MainWindow.ClosingCommands";
        public const string ClosedCommandsContract = "PefStudio.MainWindow.ClosedCommands";

        [ImportMany(MainMenuContract, typeof(IMenuItem), AllowRecomposition = true)]
        private IEnumerable<IMenuItem> MainMenuItems { get; set; }

        [ImportMany(MainToolStripsContract, typeof(IToolStrip), AllowRecomposition = true)]
        private IEnumerable<IToolStrip> MainToolStrips { get; set; }

        [ImportMany(MainStatusBarContract, typeof(IStatusBarItem), AllowRecomposition = true)]
        private IEnumerable<IStatusBarItem> MainStatusBarItems { get; set; }

        [ImportMany(LoadedCommandsContract, typeof(ICommand), AllowRecomposition = true)]
        private IEnumerable<ICommand> LoadedCommands { get; set; }

        [ImportMany(ClosedCommandsContract, typeof(ICommand), AllowRecomposition = true)]
        private IEnumerable<ICommand> ClosedCommands { get; set; }

        [ImportMany(ClosingCommandsContract, typeof(ICancelabelCommand), AllowRecomposition = true)]
        private IEnumerable<ICancelabelCommand> ClosingCommands { get; set; }

        public MenuStrip MainMenu
        {
            get { return menuStrip; }
        }

        public StatusStrip MainStatusStrip
        {
            get { return statusStrip; }
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        private bool _hasRunLoadedCommands = false;
        public void OnImportsSatisfied()
        {
            LoggingService.Debug("OnImportsSatisfied");

            CreateService.CreateMenu(menuStrip.Items, MainMenuItems, this);
            CreateService.CreateStatusBar(statusStrip.Items, MainStatusBarItems);

            ExtensionsService.Sort(MainToolStrips);
            foreach (var toolStrip in MainToolStrips.OfType<ToolStrip>())
            {
                toolStripContainer.TopToolStripPanel.Controls.Add(toolStrip);
            }

            foreach (var item in statusStrip.Items)
            {
                if (item is StatusLabel)
                    _messageBar = item as StatusLabel;
            }
            foreach (var item in statusStrip.Items)
            {
                if (item is ProgressBar)
                    _progressBar = item as ProgressBar;
            }

            if (!_hasRunLoadedCommands) // Don't run on recomposition
            {
                _hasRunLoadedCommands = true;
                foreach (var cmd in LoadedCommands)
                    cmd.Run();
            }
        } 

        private StatusLabel _messageBar = null;
        public StatusLabel MessageBar
        {
            get { return _messageBar; }
        }
        public void SetMessage(string message, bool highlighted = false)
        {
            if (MessageBar != null) MessageBar.SetMessage(message, highlighted);
        }

        private ProgressBar _progressBar = null;
        public ProgressBar ProgressBar
        {
            get { return _progressBar; }
        }
        public void SetProgress(int value)
        {
            if (_progressBar != null) _progressBar.SetProgress(value);
        }

        private void MainWindow_FormClosed(object sender, FormClosedEventArgs e)
        {
            foreach (var cmd in ClosedCommands)
                cmd.Run();
        }

        private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (var cmd in ClosingCommands)
            {
                cmd.Run();
                if (cmd.Cancel == true)
                {
                    e.Cancel = true;
                    break;
                }
            }
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            ActivePadChanged += new EventHandler(MainWindow_ActivePadChanged);
            ActiveDocumentChanged += new EventHandler(MainWindow_ActiveDocumentChanged);
            TitleChanged += new EventHandler(MainWindow_TitleChanged);
        }

        private const string BaseTitle = "Pef Studio";
        void MainWindow_TitleChanged(object sender, EventArgs e)
        {
            Text = Title + " - " + BaseTitle;
        }

        void MainWindow_ActiveDocumentChanged(object sender, EventArgs e)
        {
            if (ActiveDocument != null)
                Title = ActiveDocument.Title;

            foreach (var item in MainMenuItems)
                item.ConditionEvaluate();

            foreach (var item in MainToolStrips)
                item.ConditionEvaluate();
        }

        void MainWindow_ActivePadChanged(object sender, EventArgs e)
        {
        }
    }
}
